package cn.tedu.charging.order.rabbitmq;

import cn.tedu.charging.order.config.RabbitMQConfiguration;
import cn.tedu.charging.order.pojo.dto.OrderMQDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(OrderMQDto message) {
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause)->{
            if (ack) {
                log.debug("消息发送成功,RabbitMQ成功收到消息,消息ID:{}",correlationData);
            }else{
                log.debug("消息发送失败,RabbitMQ没有收到消息,消息ID:{}",correlationData);
                log.debug("如果失败，应该进行重试，打日志，如果重试不成功，需要告警,监控");
                log.debug("重试，重复发送，可能会导致消息重复");
            }
        });

        rabbitTemplate.convertAndSend(RabbitMQConfiguration.EXCHANGE_NAME, RabbitMQConfiguration.ROUTING_KEY, message);
    }
}
